<?php
namespace Admin\Controller;
use Think\Controller;
class ClubController extends Controller {

    //显示用户列表方法
    public function clublistJson(){
       
       $use= $this->users=M('club')->select();
   echo json_encode($use);
      } 
  
    public function clubOneJson($id){
       
       $club = M('club') -> where("clubid='$id'") -> find();
       echo json_encode($club);
      } 

    public function clubList() {
        $clubModel = M('club');
        $clubList = $clubModel -> select();

        $this -> assign('clubList',$clubList);
        $this -> display();
    }
   public function doAddClub() {
        $clubModel = M('club');
        //上传图片
        if ($_FILES['clubPic']['error'] == 0) {
            $imageInfo = $this->upload();
            $pname = $imageInfo['clubPic']['savename'];
        } else {
            $pname = "default.jpg";
        }

        $addClubData['clubid'] = NULL;
        $addClubData['clubname'] = I('post.clubname');
        $addClubData['clubaddress'] = I('post.clubaddress');
        $addClubData['clubtel'] = I('post.clubtel');
        $addClubData['clubpic'] = $pname;
        $addClubData['opentime'] = date('y-m-d H:m:s');
        $addClubData['clubtext'] = I('post.clubtext');
        $addClubData['adminid'] = session("adminId");
        $addClubData['hit'] = 0;
        $addClubData['flag'] = 1;

        $res = $clubModel -> add($addClubData);

        if ($res) {
            $this -> success("添加成功!");
        } else {
            $this -> error("添加失败!");
        }
    }
    public function updateClub($id){
        $clubModel = M('club');
        $clubData['clubid'] = $id;
        $res = $clubModel -> where($clubData) -> find();
        $this -> assign('clubInfo', $res);
        $this -> display();
    }
    public function doUpdateClub() {
        $clubModel = M('club');

        $updateClubData['clubid'] = I('post.clubid');
        $updateClubData['clubname'] = I('post.clubname');
        $updateClubData['clubaddress'] = I('post.clubaddress');
        $updateClubData['clubtel'] = I('post.clubtel');
        $updateClubData['opentime'] = date('y-m-d H:m:s');
        $updateClubData['clubtext'] = I('post.clubtext');
        $updateClubData['adminid'] = session("adminId");
        $updateClubData['hit'] = 0;
        $updateClubData['flag'] = 1;

        $res = $clubModel -> save($updateClubData);

        if ($res) {
            $this -> success("修改成功!",U('Club/clubList'));
        } else {
            $this -> error("修改失败!");
        }
    }
       public function delclub($id){
       $res= M("club") -> where("clubid = '$id'") -> delete();
       if ($res) {
            $this -> success("删除成功!",U('Club/clubList'));
        } else {
            $this -> error("修改失败!");
        }
    }
    public function recommendclub ($id, $flag) {
        $subFlag = 0;
        if ($flag == 1) {
            $subFlag = 0;
        } else {
            $subFlag = 1;
        }
        $clubData['flag'] = $subFlag;
        $res = M('club') -> where("clubid = '$id'") -> save($clubData);
        if ($res) {
            $this -> success("更改成功!",U('Club/clubList'));
        } else {
            $this -> error("更改失败!");
        }
    }

    

    //  public function index(){
    //     $share = M('share');
    //     //用户名条件搜索
    //     if(!empty($_GET['name'])){
    //       $where['name'] = array('like','%'.$_GET['name'].'%');
    //     }
    //     $total=$user->where($where)->count();
        
    //     //实例化分页类
    //     $page=new \Think\Page($total,5);
    //     $this->assign('show',$page->show());
    //     $data=$user->where($where)->page(I('get.p',1),5)->select();
    //     $this->assign('data',$data);
    //     $this->display();
    // }
    public function Upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/Club/'; // 设置附件上传根目录
      //  $upload->savePath  =   'Banner/'; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $info = $upload->upload(); // 上传文件

        if (!$info) {
            $this->error($upload->getError());
        } else {
            return $info;
        }
    }
}
